package com.cr.sync.service.impl;

import com.cr.common.utils.DateUtils;
import com.cr.sync.domain.TbSyncDetectionSampling;
import com.cr.sync.domain.vo.CountVo;
import com.cr.sync.domain.vo.detectionSampling.*;
import com.cr.sync.mapper.TbSyncDetectionSamplingMapper;
import com.cr.sync.service.ITbSyncDetectionSamplingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 远程数据-检测信息Service业务层处理
 * 
 * @author tang
 * @date 2025-08-29
 */
@Service
public class TbSyncDetectionSamplingServiceImpl implements ITbSyncDetectionSamplingService 
{
    @Resource
    private TbSyncDetectionSamplingMapper tbSyncDetectionSamplingMapper;

    @Override
    public void insertBatch(List<TbSyncDetectionSampling> data) {
//        tbSyncDetectionSamplingMapper.deleteBatch(data);
        tbSyncDetectionSamplingMapper.insertBatch(data);
    }

    @Override
    public SamplingCountResult getDetectionSamplingCount(String area) {
        int lxjc = 0;
        int lxjcFinish = 0;
        int zxjc = 0;
        int zxjcFinish = 0;
        int jdcc = 0;
        int jdccFinish = 0;

        // 检验数量统计
        List<SamplingCountVo> samplingCountVos = tbSyncDetectionSamplingMapper.selectDetectionSamplingCount(area);
        if(samplingCountVos != null && !samplingCountVos.isEmpty()) {
            for (SamplingCountVo entity : samplingCountVos) {
                String type = entity.getType();
                String result = entity.getResult();
                Integer num = entity.getNum();

                // 例行检测
                if("0".equals(type)) {
                    lxjc += num;
                    if("0".equals(result) || "1".equals(result)) {
                        lxjcFinish += num;
                    }
                }
                // 专项检测
                if("1".equals(type)) {
                    zxjc += num;
                    if("0".equals(result) || "1".equals(result)) {
                        zxjcFinish += num;
                    }
                }
                // 监督抽查
                if("2".equals(type)) {
                    jdcc += num;
                    if("0".equals(result) || "1".equals(result)) {
                        jdccFinish += num;
                    }
                }
            }
        }
        TotalCount totalCount = new TotalCount();
        totalCount.setLxjc(lxjc);
        totalCount.setLxjcFinish(lxjcFinish);
        totalCount.setZxjc(zxjc);
        totalCount.setZxjcFinish(zxjcFinish);
        totalCount.setJdcc(jdcc);
        totalCount.setJdccFinish(jdccFinish);

        // 抽样场所
        List<CountVo> countVos = tbSyncDetectionSamplingMapper.selectSamplingPlaceCount(area);

        // 年份统计
        List<DetectionSamplingYearCount> detectionSamplingYearCounts = new ArrayList<>();
        List<YearCount> yearCounts = tbSyncDetectionSamplingMapper.selectYearCount(area);
        List<String> pastYears = DateUtils.getPastYears(10);
        for (String year : pastYears) {
            List<YearCount> collect = yearCounts.stream().filter(entity -> year.equals(entity.getYear())).collect(Collectors.toList());
            if (!collect.isEmpty()) {
                DetectionSamplingYearCount detectionSamplingYearCount = new DetectionSamplingYearCount(year, 0, 0, 0);
                for (YearCount entity : collect) {
                    Integer total = entity.getTotal();

                    // 任务类型 (0, 例行检测),(1, 专项检测),(2, 监督抽查)
                    String type = entity.getType();
                    if ("0".equals(type)) {
                        detectionSamplingYearCount.setLxjc(total);
                    } else if ("1".equals(type)) {
                        detectionSamplingYearCount.setZxjc(total);
                    } else if ("2".equals(type)) {
                        detectionSamplingYearCount.setJdcc(total);
                    }
                }
                detectionSamplingYearCounts.add(detectionSamplingYearCount);
            }
        }

        SamplingCountResult result = new SamplingCountResult();
        result.setTotalCount(totalCount);
        result.setPlaceCount(countVos);
        result.setYearCount(detectionSamplingYearCounts);
        return result;
    }

}
